﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC4Shop.Models;
using System.Data;
using System.Data.SqlClient;

namespace MVC4Shop.dao
{
    public class CategoryDAO
    {
       
            public static List<CategoryModel> GetAll()
            {
                return Convert(DataUtil.executeStore("sp_Category_GetAll", null));
            }

            public static Boolean Insert(CategoryModel _model)
            {
                SqlParameter[] param = new SqlParameter[2];
                int i = 0;
                param[i] = new SqlParameter("@name", SqlDbType.NVarChar);
                param[i++].Value = _model.name;
                param[i] = new SqlParameter("@image", SqlDbType.NVarChar);
                param[i++].Value = _model.image;
                return DataUtil.executeNonStore("sp_Category_Insert", param);
            }

            public static Boolean Delete(String id)
            {
                SqlParameter[] param = new SqlParameter[1];
                int i = 0;
                param[i] = new SqlParameter("@id", SqlDbType.NVarChar);
                param[i++].Value = id;
                return DataUtil.executeNonStore("sp_Category_Delete", param);
            }

            public static List<CategoryModel> GetById(String id)
            {
                SqlParameter[] param = new SqlParameter[1];
                int i = 0;
                param[i] = new SqlParameter("@id", SqlDbType.NVarChar);
                param[i++].Value = id;
                return Convert( DataUtil.executeStore("sp_Category_GetById", param));
            }

            public static Boolean Edit(CategoryModel model)
            {
                SqlParameter[] param = new SqlParameter[2];
                int i = 0;
                param[i] = new SqlParameter("@id", SqlDbType.NVarChar);
                param[i++].Value = model.id;
                param[i] = new SqlParameter("@name", SqlDbType.NVarChar);
                param[i++].Value = model.name;
                return DataUtil.executeNonStore("sp_Category_Edit", param);
            }

            public static List<CategoryModel> Convert(DataTable dt)
            {
                List<CategoryModel> list = new List<CategoryModel>();
                CategoryModel model;
                foreach (DataRow row in dt.Rows)
                {
                    model = new CategoryModel();
                    model.id = row["category_id"].ToString();
                    model.name = row["name"].ToString();
                    model.image = row["image"].ToString();
                    list.Add(model);
                }
                return list;
            }
        
    }
}